﻿using BussinessObject.ObjectTwo;
using Class_Entity.ObjectTwo;
using ProjectHK3.App_Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectHK3.ObjectTwo
{
    public partial class OrderInternalDetail : System.Web.UI.Page
    {
        B_LoadDrop bl = new B_LoadDrop();
        Orders or = new Orders();
        B_Search ex = new B_Search();
        B_Update gx = new B_Update();
        B_Cal kx = new B_Cal();
        C_Calculator lx = new C_Calculator();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                load();
            }
        }
        public void load()
        {
            or = ex.ORDERSDETAILB(int.Parse(Session["Order"].ToString()));

            DropDownList10.SelectedValue = or.STATUS;

            DropDownList7.DataSource = bl.service();
            DropDownList7.DataTextField = "SERVICE_NAME";
            DropDownList7.DataValueField = "SERVICE_ID";
            DropDownList7.SelectedValue = or.SERVICE_ID.ToString();
            DropDownList7.DataBind();

            DropDownList8.DataSource = bl.Bran();
            DropDownList8.DataTextField = "BRANCH_NAME";
            DropDownList8.DataValueField = "BRANCH_ID";
            DropDownList8.SelectedValue = or.BRANCH_ID_FROM.ToString();
            DropDownList8.DataBind();

            DropDownList9.DataSource = bl.Bran();
            DropDownList9.DataTextField = "BRANCH_NAME";
            DropDownList9.DataValueField = "BRANCH_ID";
            DropDownList9.SelectedValue = or.BRANCH_ID_TO.ToString();
            DropDownList9.DataBind();

             TextBox1.Text = or.ORDER_NUMBER;
            TextBox20.Text = or.ORDER_DATE;
            TextBox14.Text = or.MAIL_CODE;
            TextBox15.Text = or.WEIGHT.ToString();
            TextBox19.Text = or.DESCRIPTION;
            TextBox16.Text = or.TOTAL_DELIVERY_FEE.ToString();
            TextBox17.Text = or.TOTAL_DELIVERY_FEE.ToString();

        }
        public void calB()
        {
            lx = kx.Dis(int.Parse(DropDownList8.SelectedValue.ToString()), int.Parse(DropDownList9.SelectedValue.ToString()));
            int vala;
            vala = int.Parse(lx.KC.ToString());
            TextBox16.Text = vala.ToString();
            lx = kx.FEE(vala, int.Parse(DropDownList7.SelectedValue.ToString()));
            float a1, a2, a3;
            a1 = lx.FEE_PER_KG;
            a2 = lx.FEE_PER_KM;
            a3 = (vala * a2) + (a1 * float.Parse(TextBox16.Text.ToString()));
            TextBox17.Text = a3.ToString();
        }
        public void Update()
        {
            or.ORDER_NUMBER = TextBox1.Text;
            or.ORDER_DATE = DateTime.Now.ToString("dd/MM/yyyy");
            or.IS_INTERNAL_DELIVERY = 1;
            or.ACCOUNT_ID = int.Parse(Session["Userid"].ToString());
            or.MAIL_CODE = TextBox14.Text;
            or.WEIGHT = float.Parse(TextBox15.Text.ToString());
            or.DESCRIPTION = TextBox19.Text;
            or.SERVICE_ID = int.Parse(DropDownList7.SelectedValue.ToString());
            or.BRANCH_ID_FROM = int.Parse(DropDownList8.SelectedValue.ToString());
            or.BRANCH_ID_TO = int.Parse(DropDownList9.SelectedValue.ToString());
            or.TOTAL_DELIVERY_FEE = float.Parse(TextBox16.Text);
            or.TOTAL_PAID = float.Parse(TextBox17.Text);
            or.STATUS = DropDownList10.SelectedValue.ToString();
            or.ORDER_ID = int.Parse(Session["Order"].ToString());
            or.RECEIVE_DATE = TextBox21.Text;
            if (gx.UpdateOrderB(or) == true)
            {
                Alert.Show("Update Successful");
            }
            else
            {
                Alert.Show("Update fail");
            }
        }
        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            Update();
        }

        protected void DropDownList9_SelectedIndexChanged(object sender, EventArgs e)
        {
            calB();
        }
    }
}